package com.chenjt.one_base;

import com.chenjt.three_tools.ModelUtil;
import dev.langchain4j.data.message.AiMessage;
import dev.langchain4j.model.StreamingResponseHandler;
import dev.langchain4j.model.openai.OpenAiStreamingChatModel;

import java.util.concurrent.TimeUnit;

/**
 * 注释：
 *
 * @author chenjt
 * @date 2025/3/6 15:17
 */
public class StreamingDemo {


    public static void main(String[] args) {

        OpenAiStreamingChatModel model = OpenAiStreamingChatModel.builder()
                .apiKey(ModelUtil.API_KEY)
                .modelName("gpt-4o-mini")
//                .baseUrl("https://api.gptsapi.net/v1")
                /**
                 * 连不到
                 * java.net.ConnectException: Failed to connect to api.openai.com/108.160.165.53:443
                 */
//                .baseUrl("https://api.openai.com/v1")
//                .baseUrl("https://api.openai.com/v1")
                .build();

        model.generate("你好，你是谁？", new StreamingResponseHandler<AiMessage>() {
            @Override
            public void onNext(String s) {
                System.out.println(s);
                try {
                    TimeUnit.SECONDS.sleep(1);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onError(Throwable error) {
                System.out.println(error);
            }
        });

    }


}
